// using UnityEngine;
// using System.Collections.Generic;
//
// /// <summary>
// /// AssetBundle资源管理器，负责加载、缓存和释放AssetBundle资源
// /// </summary>
// public class AssetBundleResourceManager : SingletonMono<AssetBundleResourceManager>
// {
//     // 已加载的AssetBundle缓存
//     private Dictionary<string, AssetBundle> loadedAssetBundles = new Dictionary<string, AssetBundle>();
//     // 引用计数，用于跟踪每个AssetBundle的引用数量
//     private Dictionary<string, int> referenceCounts = new Dictionary<string, int>();
//     // 主AssetBundle
//     private AssetBundle mainAssetBundle;
//     // 主AssetBundle的清单
//     private AssetBundleManifest manifest;
//
//     /// <summary>
//     /// 初始化资源管理器，加载主AssetBundle和清单
//     /// </summary>
//     public void Initialize()
//     {
//         string mainAssetBundleName = GetPlatformName();
//         string mainAssetBundlePath = GetLocalAssetBundlePath(mainAssetBundleName);
//         
//         // 加载主AssetBundle
//         mainAssetBundle = AssetBundle.LoadFromFile(mainAssetBundlePath);
//         if (mainAssetBundle == null)
//         {
//             Debug.LogError("无法加载主AssetBundle: " + mainAssetBundlePath);
//             return;
//         }
//         
//         // 加载清单
//         manifest = mainAssetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//         if (manifest == null)
//         {
//             Debug.LogError("无法加载AssetBundle清单");
//             return;
//         }
//         
//         Debug.Log("资源管理器初始化完成");
//     }
//
//     /// <summary>
//     /// 异步加载AssetBundle中的资源
//     /// </summary>
//     /// <typeparam name="T">资源类型</typeparam>
//     /// <param name="assetBundleName">AssetBundle名称</param>
//     /// <param name="assetName">资源名称</param>
//     /// <param name="callback">加载完成回调</param>
//     public void LoadAssetAsync<T>(string assetBundleName, string assetName, System.Action<T> callback) where T : Object
//     {
//         StartCoroutine(LoadAssetBundleAndAssetAsync<T>(assetBundleName, assetName, callback));
//     }
//
//     /// <summary>
//     /// 异步加载AssetBundle及其依赖项，并从中加载资源
//     /// </summary>
//     private System.Collections.IEnumerator LoadAssetBundleAndAssetAsync<T>(string assetBundleName, string assetName, System.Action<T> callback) where T : Object
//     {
//         // 获取所有依赖项
//         string[] dependencies = manifest.GetAllDependencies(assetBundleName);
//         
//         // 先加载所有依赖项
//         foreach (string dependency in dependencies)
//         {
//             yield return LoadAssetBundleAsync(dependency);
//         }
//         
//         // 再加载主AssetBundle
//         yield return LoadAssetBundleAsync(assetBundleName);
//         
//         // 从AssetBundle中加载资源
//         if (loadedAssetBundles.TryGetValue(assetBundleName, out AssetBundle assetBundle))
//         {
//             AssetBundleRequest request = assetBundle.LoadAssetAsync<T>(assetName);
//             yield return request;
//             
//             if (callback != null)
//                 callback(request.asset as T);
//         }
//         else
//         {
//             Debug.LogError("AssetBundle未找到: " + assetBundleName);
//             if (callback != null)
//                 callback(null);
//         }
//     }
//
//     /// <summary>
//     /// 异步加载AssetBundle
//     /// </summary>
//     private System.Collections.IEnumerator LoadAssetBundleAsync(string assetBundleName)
//     {
//         // 如果已经加载，则增加引用计数并返回
//         if (loadedAssetBundles.ContainsKey(assetBundleName))
//         {
//             referenceCounts[assetBundleName]++;
//             yield break;
//         }
//         
//         string assetBundlePath = GetLocalAssetBundlePath(assetBundleName);
//         
//         // 从文件异步加载AssetBundle
//         AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(assetBundlePath);
//         yield return request;
//         
//         if (request.assetBundle != null)
//         {
//             loadedAssetBundles.Add(assetBundleName, request.assetBundle);
//             referenceCounts.Add(assetBundleName, 1);
//             Debug.Log("成功加载AssetBundle: " + assetBundleName);
//         }
//         else
//         {
//             Debug.LogError("无法加载AssetBundle: " + assetBundlePath);
//         }
//     }
//
//     /// <summary>
//     /// 释放AssetBundle资源
//     /// </summary>
//     /// <param name="assetBundleName">AssetBundle名称</param>
//     /// <param name="unloadAllLoadedObjects">是否卸载所有已加载的对象</param>
//     public void UnloadAssetBundle(string assetBundleName, bool unloadAllLoadedObjects = false)
//     {
//         if (!loadedAssetBundles.ContainsKey(assetBundleName))
//             return;
//         
//         // 减少引用计数
//         referenceCounts[assetBundleName]--;
//         
//         // 当引用计数为0时卸载AssetBundle
//         if (referenceCounts[assetBundleName] <= 0)
//         {
//             if (loadedAssetBundles[assetBundleName] != null)
//             {
//                 loadedAssetBundles[assetBundleName].Unload(unloadAllLoadedObjects);
//                 loadedAssetBundles.Remove(assetBundleName);
//                 referenceCounts.Remove(assetBundleName);
//                 Debug.Log("成功卸载AssetBundle: " + assetBundleName);
//             }
//         }
//     }
//
//     /// <summary>
//     /// 获取平台名称
//     /// </summary>
//     private string GetPlatformName()
//     {
//         #if UNITY_EDITOR
//         return GetPlatformForAssetBundles(UnityEditor.EditorUserBuildSettings.activeBuildTarget);
//         #else
//         return GetPlatformForAssetBundles(Application.platform);
//         #endif
//     }
//
//     #if UNITY_EDITOR
//     /// <summary>
//     /// 获取编辑器中构建目标对应的平台名称
//     /// </summary>
//     private string GetPlatformForAssetBundles(UnityEditor.BuildTarget target)
//     {
//         switch (target)
//         {
//             case UnityEditor.BuildTarget.Android:
//                 return "Android";
//             case UnityEditor.BuildTarget.iOS:
//                 return "iOS";
//             case UnityEditor.BuildTarget.StandaloneWindows:
//             case UnityEditor.BuildTarget.StandaloneWindows64:
//                 return "Windows";
//             case UnityEditor.BuildTarget.StandaloneOSX:
//                 return "OSX";
//             default:
//                 return null;
//         }
//     }
//     #else
//     /// <summary>
//     /// 获取运行时平台对应的平台名称
//     /// </summary>
//     private string GetPlatformForAssetBundles(RuntimePlatform platform)
//     {
//         switch (platform)
//         {
//             case RuntimePlatform.Android:
//                 return "Android";
//             case RuntimePlatform.IPhonePlayer:
//                 return "iOS";
//             case RuntimePlatform.WindowsPlayer:
//                 return "Windows";
//             case RuntimePlatform.OSXPlayer:
//                 return "OSX";
//             default:
//                 return null;
//         }
//     }
//     #endif
//
//     /// <summary>
//     /// 获取本地AssetBundle路径
//     /// </summary>
//     private string GetLocalAssetBundlePath(string assetBundleName)
//     {
//         return System.IO.Path.Combine(Application.persistentDataPath, assetBundleName);
//     }
// }    